﻿using System;
using System.IO;
using System.Collections.Generic;

namespace SerialManager
{
    /// <summary>
    /// Class wraps list of video a subtitle files that belongs to one episode (identified by <see cref="EpisodeFiles.Match"/>
    /// </summary>
    class EpisodeFiles
    {
        internal readonly static string[] VideoExtensions = new string[] { "avi", "mpg", "wmv", "mkv", "mp4" };
        internal readonly static string[] SubtitleExtensions = new string[] { "srt", "sub", "txt" };

        private readonly List<FileInfo> _videos = new List<FileInfo>();
        private readonly List<FileInfo> _subtitles = new List<FileInfo>();

        /// <summary>
        /// Pattern match that identifies this episode
        /// </summary>
        public EpisodePatternMatch Match { get; private set; }

        /// <summary>
        /// Informations about episode got from <see cref="Loaders.IEpisodeLoader"/>
        /// </summary>
        public EpisodeInfo Info { get; private set; }

        /// <summary>
        /// List of video files
        /// </summary>
        public FileInfo[] Videos { get { return _videos.ToArray(); } }

        /// <summary>
        /// List of subtitle files
        /// </summary>
        public FileInfo[] Subtitles { get { return _subtitles.ToArray(); } }

        /// <summary>
        /// Only possible constructor of class
        /// </summary>
        /// <param name="match">Match information about episode</param>
        public EpisodeFiles(EpisodePatternMatch match)
        {
            Match = match;
        }

        private EpisodeFiles() { }

        /// <summary>
        /// Add file that belongs to this episode
        /// It's automatically sorted to right group by file extension
        /// </summary>
        /// <param name="file"></param>
        public void Add( FileInfo file )
        {
            if( file.HasAnyExtension( VideoExtensions ) )
                _videos.Add( file );
            else if (file.HasAnyExtension( SubtitleExtensions ))
                _subtitles.Add( file );
        }

        /// <summary>
        /// Prepares list of file for renaming operation
        /// </summary>
        /// <param name="forMove">Kolekce, do ktere jsou zapsany soubory k presunu</param>
        public void PrepareMove( ref FileListBuilder.MoveResult result )
        {
            var path = Path.Combine( Core.SerialDirectory.FullName, "Season " + Info.Season );
            var name = String.Format( "{0} - {1:00}x{2:00}",
                Core.SerialTitle,
                Match.Season, Info.Number
            );
            if (Info != null && !String.IsNullOrEmpty(Info.Title))
                name += " - " + Core.FixFileName(Info.Title);

            var fullpath = Path.Combine( path, name );

            foreach( var video in Videos )
                result.Add( video, fullpath );
            
            foreach( var sub in Subtitles )
                result.Add( sub, fullpath );
        }

        /// <summary>
        /// Unique hash code for this episode
        /// </summary>
        public override int GetHashCode()
        {
            return Match.GetHashCode();
        }

        /// <summary>
        /// Compares two instances of <see cref="EpisodeFiles"/>
        /// It can also be compared to <see cref="EpisodePatternMatch"/> and <see cref="EpisodeInfo"/>
        /// </summary>
        public override bool Equals( object obj )
        {
            return Match.Equals(obj);
        }
    }
}
